package com.yytek.nmp.controller;

import java.util.List;
import java.util.Map;

import com.yytek.common.core.domain.R;
import com.yytek.common.security.utils.SecurityUtils;
import com.yytek.common.core.domain.DeviceCache;
import com.yytek.nmp.dto.DeviceServiceInvoke;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yytek.common.log.annotation.Log;
import com.yytek.common.log.enums.BusinessType;
import com.yytek.common.security.annotation.RequiresPermissions;
import com.yytek.nmp.domain.IotDeviceInfo;
import com.yytek.nmp.service.IIotDeviceInfoService;
import com.yytek.common.core.web.controller.BaseController;
import com.yytek.common.core.web.domain.AjaxResult;
import com.yytek.common.core.utils.poi.ExcelUtil;
import com.yytek.common.core.web.page.TableDataInfo;

/**
 * 设备管理Controller
 * 
 * @author yytek-iot
 * @date 2022-07-05
 */
@Api(value = "设备管理控制器", tags = {"设备管理管理"})
@RestController
@RequestMapping("/device")
public class IotDeviceInfoController extends BaseController
{
    @Autowired
    private IIotDeviceInfoService iotDeviceInfoService;

    /**
     * 设备服务调用
     * @param deviceServiceInvoke
     * @return
     */
    @ApiOperation("设备服务调用")
    @PostMapping("/invokeDeviceService")
    public R invokeDeviceService(@RequestBody DeviceServiceInvoke deviceServiceInvoke)
    {
        iotDeviceInfoService.invokeDeviceService(deviceServiceInvoke);
        return R.ok();
    }

    /**
     * 发送设备升级信息
     * @param iotId 设备id
     * @param upgradeStrategyId 升级策略id
     * @return
     */
    @ApiOperation("发送设备升级信息")
    @PostMapping("/sendDeviceUpgradeMsg")
    public R sendDeviceUpgradeMsg(String iotId,String upgradeStrategyId)
    {
        iotDeviceInfoService.sendDeviceUpgradeMsg(iotId,upgradeStrategyId);
        return R.ok();
    }

    /**
     * 查询设备相关缓存信息
     */
    @ApiOperation("查询设备相关缓存信息")
    @PostMapping("/getDeviceCache")
    public R<DeviceCache> getDeviceCache(String id)
    {
        return R.ok(iotDeviceInfoService.getDeviceCacheByIotId(id));
    }

    /**
     * 网关解绑子设备
     */
    @ApiOperation("网关解绑子设备")
    @PostMapping("/noBindTopoDevice")
    public AjaxResult noBindTopoDevice(String gateWayid,String subId)
    {
        return toAjax(iotDeviceInfoService.noBindTopoDevice(gateWayid,subId));
    }

    /**
     * 网关绑定子设备
     */
    @ApiOperation("网关绑定子设备")
    @PostMapping("/bindTopoDevice")
    public AjaxResult bindTopoDevice(String gateWayid,String subId)
    {
        return toAjax(iotDeviceInfoService.bindTopoDevice(gateWayid,subId));
    }

    /**
     * 分页查询网关没有绑定的子设备列表
     */
    @ApiOperation("分页查询网关没有绑定的子设备列表")
    @GetMapping("/getNoBindTopoDevice")
    public TableDataInfo getNoBindTopoDevice(String id)
    {
        startPage();
        List<IotDeviceInfo> list = iotDeviceInfoService.getNoBindTopoDevice(id);
        return getDataTable(list);
    }

    /**
     * 查询网关子设备列表
     */
    @ApiOperation("查询网关子设备列表")
    @GetMapping("/getTopoDevice")
    public AjaxResult getTopoDevice(String id)
    {
        List<IotDeviceInfo> list = iotDeviceInfoService.getTopoDevice(id);
        return AjaxResult.success(list);
    }

    /**
     * 查询设备的历史实时属性数据
     */
    @ApiOperation("查询设备的历史实时属性数据")
    @GetMapping("/getHisProperties")
    public AjaxResult getHisProperties(String id,String startTime,String endTime)
    {
        List<Map<String,Object>> list = iotDeviceInfoService.getHisProperties(id,startTime,endTime);
        return AjaxResult.success(list);
    }

    /**
     * 查询当前设备的最新属性数据
     */
    @ApiOperation("查询当前设备的最新属性数据")
    @GetMapping("/getProperties")
    public AjaxResult getProperties(String id)
    {
        List<Map<String,Object>> list = iotDeviceInfoService.getPropertiesByIotId(id);
        return AjaxResult.success(list);
    }

    /**
     * 分页查询授权项目的设备管理列表
     */
    @ApiOperation("分页查询授权项目的设备管理列表")
    @RequiresPermissions("nmp:device:list")
    @GetMapping("/authList")
    public TableDataInfo authList(IotDeviceInfo iotDeviceInfo)
    {
        startPage();
        String userId = SecurityUtils.getUserId().toString();
        List<IotDeviceInfo> list = iotDeviceInfoService.selectDeviceInfoAuthList(iotDeviceInfo,userId);
        return getDataTable(list);
    }
    /**
     * 查询所有设备列表
     */
    @ApiOperation("查询所有设备列表")
    @PostMapping("/selectDeviceInfoList")
    public R<List<IotDeviceInfo>> selectDeviceInfoList(IotDeviceInfo iotDeviceInfo)
    {
        List<IotDeviceInfo> list = iotDeviceInfoService.selectIotDeviceInfoList(iotDeviceInfo);
        return R.ok(list);
    }


    /**
     * 分页查询自己项目的设备管理列表
     */
    @ApiOperation("分页查询自己项目的设备管理列表")
    @RequiresPermissions("nmp:device:list")
    @GetMapping("/myList")
    public TableDataInfo myList(IotDeviceInfo iotDeviceInfo)
    {
        startPage();
        String userId = SecurityUtils.getUserId().toString();
        List<IotDeviceInfo> list = iotDeviceInfoService.selectDeviceInfoMyList(iotDeviceInfo,userId);
        return getDataTable(list);
    }

    /**
     * 导出设备管理列表
     */
    @ApiOperation("导出设备管理列表")
    @RequiresPermissions("nmp:device:export")
    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IotDeviceInfo iotDeviceInfo)
    {
        List<IotDeviceInfo> list = iotDeviceInfoService.selectIotDeviceInfoList(iotDeviceInfo);
        ExcelUtil<IotDeviceInfo> util = new ExcelUtil<IotDeviceInfo>(IotDeviceInfo.class);
        util.exportExcel(response, list, "设备管理数据");
    }

    /**
     * 获取设备管理详细信息
     */
    @ApiOperation("获取设备管理详细信息")
    @RequiresPermissions("nmp:device:query")
    @GetMapping(value = "/{iotId}")
    public AjaxResult getInfo(@PathVariable("iotId") String iotId)
    {
        return AjaxResult.success(iotDeviceInfoService.selectIotDeviceInfoByIotId(iotId));
    }

    /**
     * 新增设备管理
     */
    @ApiOperation("新增设备管理")
    @RequiresPermissions("nmp:device:add")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IotDeviceInfo iotDeviceInfo)
    {
        return iotDeviceInfoService.insertDevice(iotDeviceInfo);
    }

    /**
     * 修改设备管理
     */
    @ApiOperation("修改设备管理")
    @RequiresPermissions("nmp:device:edit")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IotDeviceInfo iotDeviceInfo)
    {
        return toAjax(iotDeviceInfoService.updateIotDeviceInfo(iotDeviceInfo));
    }

    /**
     * 删除设备管理
     */
    @ApiOperation("删除设备管理")
    @RequiresPermissions("nmp:device:remove")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{iotIds}")
    public AjaxResult remove(@PathVariable String iotIds)
    {
        return iotDeviceInfoService.deleteDeviceByIds(iotIds);
    }
}
